package com.zg.lotterycrawler.util;

import com.zg.lotterycrawler.entity.Lottery;
import com.zg.lotterycrawler.mapper.LotteryMapper;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author 港某人
 * @date 2021/6/26 22:02
 */
@Slf4j
@Component
public class AppRun implements ApplicationRunner {

    private static int startYear = 11;
    private static int startInstallment = 0;
    private static String currentInstallment = "";
    private static final String INIT_URL = "https://kaijiang.500.com/shtml/ssq/";

    @Resource
    LotteryMapper lotteryMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        do {
            String url = getUrl();
            log.info(url);
            Document document = null;
            try {
                document = Jsoup.connect(url)
                        .ignoreContentType(true)
                        .ignoreHttpErrors(true)
                        .header("cookie", "ck_regchanel=baidu; regfrom=0%7Cala%7Cbaidu; sdc_session=1624699596739; seo_key=baidu%7C%7Chttps://www.baidu.com/link?url=Wk92H5mIXEN7d7w0CyEbetwqeAmhIksPULzb43zdrMkCqW_vVUkaqqwt_IMYhCG9&wd=&eqid=b5db41b8000577d70000000560d6f2c5; bdshare_firstime=1624699596816; ck_RegUrl=kaijiang.500.com; ck_RegFromUrl=http%3A//kaijiang.500.com/%3F0_ala_baidu; motion_id=1624699784012_0.9701768238917066; WT_FPC=id=undefined:lv=1624699828806:ss=1624699596738; sdc_userflag=1624699596739::1624699828808::19; CLICKSTRN_ID=112.3.226.156-1624699592.298734::DED24DFBD1E989CAD215EEAAD22CD53E")
                        .get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (document == null) {
                continue;
            }
            try {
                Elements li = document
                        .getElementsByClass("ball_box01")
                        .get(0)
                        .getElementsByTag("li");
                Lottery lottery = new Lottery();
                lottery.setRed1(Integer.parseInt(li.get(0).text()));
                lottery.setRed2(Integer.parseInt(li.get(1).text()));
                lottery.setRed3(Integer.parseInt(li.get(2).text()));
                lottery.setRed4(Integer.parseInt(li.get(3).text()));
                lottery.setRed5(Integer.parseInt(li.get(4).text()));
                lottery.setRed6(Integer.parseInt(li.get(5).text()));
                lottery.setBlue1(Integer.parseInt(li.get(6).text()));
                lottery.setInstallment(currentInstallment);
                lottery.setOriginUrl(url);
                lotteryMapper.insert(lottery);
            } catch (Exception e) {
                log.error(e.getMessage());
                continue;
            }
            //等待0.5秒
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        } while (startYear <= 21);
    }

    String getUrl() {
        if (startInstallment >= 154) {
            startYear++;
            startInstallment = 0;
        }
        startInstallment++;
        currentInstallment = String.format("%02d", startYear) + String.format("%03d", startInstallment);
        return INIT_URL + currentInstallment + ".shtml";
    }

}
